/*
 * Copyright (C) 2012 Le Tuan Anh <anh.takaji@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ezcms.ejb;

import ezcms.constants.AuthenticationStatus;
import ezcms.dal.client.DAOUtil;
import ezcms.dto.User;
import ezcms.exceptions.BusinessLogicException;
import ezcms.exceptions.DataLayerException;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateless;

/**
 *
 * @author Le Tuan Anh <anh.takaji@gmail.com>
 */
@Stateless(mappedName = "UserManager")
public class UserManagerBean implements UserManagerBeanLocal {

    private DAOUtil dao;

    @PostConstruct
    public void initialize() {
        dao = new DAOUtil();
    }

    public boolean authenticate(User user) throws BusinessLogicException {
        try {
            return dao.getUserDAO().authenticate(user) == AuthenticationStatus.VALID_USER;
        } catch (DataLayerException ex) {
            throw new BusinessLogicException(ex);
        }
    }

    @Override
    public User getUser(String email) throws BusinessLogicException {
        try {
            return dao.getUserDAO().getUser(email);
        } catch (DataLayerException ex) {
            throw new BusinessLogicException(ex);
        }
    }

    @Override
    public void saveUser(User user) throws BusinessLogicException {
        try {
            dao.getUserDAO().saveUser(user);
        } catch (DataLayerException ex) {
            throw new BusinessLogicException(ex);
        }
    }

    @Override
    public void deleteUser(User user) throws BusinessLogicException {
        try {
            dao.getUserDAO().deleteUser(user);
        } catch (DataLayerException ex) {
            throw new BusinessLogicException(ex);
        }
    }

    @Override
    public List<User> getAllUsers() throws BusinessLogicException {
        try {
            return dao.getUserDAO().getAllUsers();
        } catch (DataLayerException ex) {
            throw new BusinessLogicException(ex);
        }
    }
}
